package com.dexgdx.game.manager;

import java.util.ArrayList;

import com.dexgdx.game.Game;
import com.dexgdx.game.actor.AbstractActor;
import com.dexgdx.game.actor.Enemy;
import com.dexgdx.game.actor.JSONActor;
import com.dexgdx.game.factory.ActorFactory;

public class ActorManager {

	public static void loadActorsFromJSON2(){
		JSONManager.saveJSONActor();
	}
	public static void loadActorsFromJSON(){
		ArrayList<JSONActor> jSONActors = JSONManager.getJSONActor();
		for (JSONActor jsonActor : jSONActors) {
			addActor(ActorFactory.loadFromJSONActor(jsonActor));
		}
	}
	
	public static void addActor(AbstractActor actor){
		Long counter = new Long(++Game.actorData.actorIndexCounter);
		actor.setActorIndex(counter);
		Game.actorData.actorList.put(counter, actor);
		if(actor instanceof Enemy)
			Game.actorData.enemyList.add(counter);
		else
			Game.actorData.playerList.add(counter);
	}
	
	public static void removeActor(int actorIndex){
		Long counter = new Long(actorIndex);
		Game.actorData.actorList.remove(counter);
		Game.actorData.enemyList.remove(counter);
		Game.actorData.playerList.remove(counter);
		deSelectPlayer(actorIndex);
	}
	
	public static AbstractActor getActor(long actorIndex){
		return Game.actorData.actorList.get(actorIndex);
	}

	public static void selectPlayer(long actorIndex, boolean multiple){
		Long counter = new Long(actorIndex);
		if(!multiple){
			deSelectAllPlayers();
		}
		if(Game.actorData.playerList.contains(counter) && !Game.actorData.selectedPlayerList.contains(counter)){
			Game.actorData.actorList.get(counter).getActorStatus().setSelected(true);
			Game.actorData.selectedPlayerList.add(counter);
		}
	}
	
	public static void deSelectPlayer(long actorIndex){
		Long counter = new Long(actorIndex);
		Game.actorData.selectedPlayerList.remove(counter);
		Game.actorData.actorList.get(counter).getActorStatus().setSelected(false);
		
	}
	
	public static void selectAllPlayers(){
		for (Long playerIndex : Game.actorData.playerList) {
			selectPlayer(playerIndex, true);
		}
	}
	
	public static void deSelectAllPlayers(){
		Object[] playerToDeselect = Game.actorData.selectedPlayerList.toArray();
		for (Object playerIndex : playerToDeselect) {
			deSelectPlayer((Long)playerIndex);
		}
	}
	
	public static boolean isActorSelected(AbstractActor actor){
		return Game.actorData.selectedPlayerList.contains(actor.getActorIndex());
	}
	
	public static boolean isAnyPlayerSelected(){
		return Game.actorData.selectedPlayerList.size() > 0;
	}
}
